//
//  TestLIstController.swift
//  STest
//
//  Created by wang tiao on 2025/9/13.
//

import UIKit
import SnapKit

class TestListModel {
    
    
}

class TestListCell: UITableViewCell {
    
    
    var model: TestListModel! {
        didSet {
            
            
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = .black
        selectionStyle = .none
        
        let backgroundContainerView = UIView()
        backgroundContainerView.addCorner(radius: 12)
        backgroundContainerView.backgroundColor = UIColorMake(0x2F323E)
        contentView.addSubview(backgroundContainerView)
        backgroundContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.top.equalToSuperview().offset(16)
            make.bottom.equalToSuperview()
        }
        
        let titleView = UIView().addTo(backgroundContainerView)
        titleView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(40)
        }
        
        let titleLineView = UIView().addTo(titleView)
        titleLineView.backgroundColor = .white.withAlphaComponent(0.3)
        titleLineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(12)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let titleLabel = UILabel(text: "#52658425628", font: UIFontMake(14), textColor: .white).addTo(titleView)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.centerY.equalToSuperview()
        }
        
        let dayLabel = GradientBorderLabel(text: "90天", font: UIFontMake(14), textColor: .white, textAlignment: .center).addTo(titleView)
        dayLabel.backgroundColor = UIColor.black
        dayLabel.gradientColors = [UIColor.systemBlue, UIColor.systemPurple, UIColor.systemPink]
        dayLabel.borderWidth = 2
        dayLabel.layer.cornerRadius = 13
        dayLabel.layer.masksToBounds = true
        dayLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(60)
            make.height.equalTo(26)
        }
        
        let dateLabel = UILabel(text: "2025-07-31 06:37 PM", font: UIFontMake(11), textColor: .white).addTo(titleView)
        dateLabel.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.centerY.equalToSuperview()
        }
        
        let startEndDateView = UIView().addTo(backgroundContainerView)
        startEndDateView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleView.snp.bottom)
            make.height.equalTo(40)
        }
        
        let startDateLabel = UILabel(text: "开始日期：2025-08-12", font: UIFontMake(13), textColor: .white).addTo(startEndDateView)
        startDateLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.centerY.equalToSuperview()
        }
        
        let endDateLabel = UILabel(text: "开始日期：2025-08-12", font: UIFontMake(13), textColor: .white).addTo(startEndDateView)
        endDateLabel.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.centerY.equalToSuperview()
        }
        
        let startEndDateLineView = UIView().addTo(startEndDateView)
        startEndDateLineView.backgroundColor = .white.withAlphaComponent(0.3)
        startEndDateLineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(12)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let infoView = UIView().addTo(backgroundContainerView)
        infoView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(startEndDateLineView.snp.bottom)
            make.bottom.equalToSuperview()
        }
        
        let yearInterestRateLabel = UILabel(text: "年利率", font: UIFontMake(15), textColor: .white).addTo(infoView)
        yearInterestRateLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.top.equalTo(18)
            make.width.equalTo(80)
        }
        
        let yearInterestRateValueLabel = UILabel(text: "94.54%", font: UIFontMake(15), textColor: .white).addTo(infoView)
        yearInterestRateValueLabel.snp.makeConstraints { make in
            make.left.equalTo(yearInterestRateLabel.snp.right).offset(10)
            make.centerY.equalTo(yearInterestRateLabel)
        }
        
        let pledgeValueLabel = UILabel(text: "质押价值", font: UIFontMake(15), textColor: .white).addTo(infoView)
        pledgeValueLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.width.equalTo(yearInterestRateLabel)
        }
        
        let pledgeValueValueLabel = UILabel(text: "$500", font: UIFontMake(15), textColor: .white).addTo(infoView)
        pledgeValueValueLabel.snp.makeConstraints { make in
            make.centerY.equalTo(pledgeValueLabel)
            make.left.equalTo(yearInterestRateValueLabel)
        }
        
        let incomeLabel = UILabel(text: "收益", font: UIFontMake(15), textColor: .white).addTo(infoView)
        incomeLabel.snp.makeConstraints { make in
            make.bottom.equalTo(-18)
            make.left.width.equalTo(yearInterestRateLabel)
        }
        
        let incomeValueLabel = UILabel(text: "$472.7000", font: UIFontMake(15), textColor: .white).addTo(infoView)
        incomeValueLabel.snp.makeConstraints { make in
            make.centerY.equalTo(incomeLabel)
            make.left.equalTo(yearInterestRateValueLabel)
        }
        
        let stateLabel = UILabel(text: "收益值未满", font: UIFontMake(15), textColor: .white, textAlignment: .center).addTo(infoView)
        stateLabel.backgroundColor = .lightGray
        stateLabel.addCorner(radius: 10)
        stateLabel.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.right.equalTo(endDateLabel)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        let progressView = GradientCircularProgressView().addTo(infoView)
        progressView.gradientColors = [UIColor.systemBlue, UIColor.systemPurple]
        progressView.lineWidth = 4                // 设置圆环宽度
        progressView.progress = 0.75               // 设置进度
        progressView.snp.makeConstraints { make in
            make.right.equalTo(endDateLabel)
            make.centerY.equalTo(incomeLabel)
            make.width.height.equalTo(30)
        }
        
        let progressLabel = UILabel(text: "完成度 40%", font: UIFontMake(15), textColor: .white, textAlignment: .center).addTo(infoView)
        progressLabel.snp.makeConstraints { make in
            make.centerY.equalTo(incomeLabel)
            make.right.equalTo(progressView.snp.left).offset(-20)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class TestLIstController: UIViewController {
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.backgroundColor = UIColor.black
        tableView.rowHeight = 200
        tableView.contentInsetAdjustmentBehavior = .never;
        tableView.register(TestListCell.self, forCellReuseIdentifier: "TestListCell")
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
        return tableView
    }()
    
    
    var datas: [TestListModel] = [
        TestListModel(),
        TestListModel(),
        TestListModel(),
        TestListModel(),
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .black
        
        self.navigationController?.navigationBar.isTranslucent = false
        
        tableView.addTo(view)
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview()
        }
    }
    
}



extension TestLIstController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TestListCell", for: indexPath) as! TestListCell
        cell.model = datas[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let model = datas[indexPath.row]
        
        
    }
    
}
